package com.shine.icis.action;

import java.net.URLEncoder;
import java.util.List;
import java.util.UUID;

import com.shine.cotrun.sql.QueryParameters;
import com.shine.cotrun.web.comm.ListPage;
import com.shine.cotrun.web.upload.FileDispatcher;
import com.shine.icis.blo.IDataCheckService;
import com.shine.icis.blo.IDataSubmitService;
import com.shine.icis.config.SysConstants;
import com.shine.icis.dto.ProgressStatus;
import com.shine.icis.hbean.DataCheckDef;
import com.shine.icis.hbean.DataCheckR13;
import com.shine.icis.hbean.R13ChkFactAmt;
import com.shine.icis.hbean.SettleAmtDetail;

public class DataCheck13Action extends BaseAction {

	/**
	 * 客户自有两类资金划转核对
	 * @author zuohp 2012-07-21
	 * */
	private static final long serialVersionUID = 7638121893867812633L;
	
	private IDataCheckService dataCheckService;
	private IDataSubmitService dataSubmitService;
	
	// input param
	private int pageNo;
	private int pageSize;
	private int id;
	private Integer relateId;
	private String chkType; //核对类型
	private double discAmt;//挪用金额
	private double reservesFactAmt; //T日客户自有资金实际划转金额
	private double fullComm;//T-1日证券公司向客户扣收的手续费
	private double accumAdjustAmt; //T-1日累计待调整自有收支
	private double sdcCommAmt;//T-1证券公司支付登记公司的一级费用(不含三板数据）
	private double bankComm;//T-1日银行收取的手续费
	private double bankInterest;//T-1证券公司客户资金存款账户收到的利息
	private double reservesInterest;//T-1证券公司客户资金存款账户收到的利息
	private double secInterest;//T-1证券公司向客户支付的结息金额
	private double withheldTaxAmt; //T-1日当日代扣代缴税费
	private double settleAdjustAmt; //T日当日交收当日已调整自有资金
	private double clearOffsetBal; //T-1日清算轧差
	private double stamptaxOffsetBal; //T-1日印花税轧差
	private double redBlueFillAmt; //T-1日红冲蓝补
	private double other1Amt; //其他费用1
	private double other2Amt; //其他费用2
	private double other3Amt; //其他费用3
	private double other4Amt; //其他费用4
	private String note;//备注
	private String busiDate;//数据日期
	private String expType;//导出方式
	private String dateType;//数据日期类型：清算日/交收日
	private String factAmtDate;//查询日期
	private String querySettleDate;//交收金额统计查询日期
	private double shFactAmt; //修改的上海实际划转金额
	private double szFactAmt; //修改的深圳实际划转金额
	private String checkDate; //查询日期
	
	//output value
	private DataCheckR13 checkR13;//R13查询结果
	private R13ChkFactAmt r13ChkFactAmt;//客户自有两类实际划转金额
	private SettleAmtDetail settleAmtDetail;//交收统计数据
	private ListPage<DataCheckR13> checkR13Page;//R13查询结果
	private List<DataCheckDef> dataCheckDefs;
	//private String currentBusiDate;//当前交易日期
	private int jobStatus;//数据核对的状态：-2未就绪；-1等待；0运行中；1结束；2结束但TS处于运行中
	private String downloadUrl;//下载地址
	
	public void setDataCheckService(IDataCheckService dataCheckService) {
		this.dataCheckService = dataCheckService;
	}
	public void setDataSubmitService(IDataSubmitService dataSubmitService) {
		this.dataSubmitService = dataSubmitService;
	}
	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public void setId(int id) {
		this.id = id;
	}
	public void setRelateId(int relateId) {
		this.relateId = relateId;
	}

	public void setChkType(String chkType) {
		this.chkType = chkType;
	}
	public void setDiscAmt(double discAmt) {
		this.discAmt = discAmt;
	}
	//T日客户自有资金实际划转金额
	public void setReservesFactAmt(double reservesFactAmt) {
		this.reservesFactAmt = reservesFactAmt;
	}
	//T-1日证券公司向客户扣收的手续费
	public void setFullComm(double fullComm) {
		this.fullComm = fullComm;
	}
	//T-1日累计待调整自有收支
	public void setAccumAdjustAmt(double accumAdjustAmt) {
		this.accumAdjustAmt = accumAdjustAmt;
	}
	//T-1证券公司支付登记公司的一级费用(不含三板数据）
	public void setSdcCommAmt(double sdcCommAmt) {
		this.sdcCommAmt = sdcCommAmt;
	}
	//T-1日银行收取的手续费
	public void setBankComm(double bankComm) {
		this.bankComm = bankComm;
	}
	//T-1证券公司客户资金存款账户收到的利息
	public void setBankInterest(double bankInterest) {
		this.bankInterest = bankInterest;
	}
	//T-1证券公司客户资金存款账户收到的利息
	public void setReservesInterest(double reservesInterest) {
		this.reservesInterest = reservesInterest;
	}
	//T-1证券公司向客户支付的结息金额
	public void setSecInterest(double secInterest) {
		this.secInterest = secInterest;
	}
	//T-1日当日代扣代缴税费
	public void setWithheldTaxAmt(double withheldTaxAmt) {
		this.withheldTaxAmt = withheldTaxAmt;
	}
	//T日当日交收当日已调整自有资金
	public void setSettleAdjustAmt(double settleAdjustAmt) {
		this.settleAdjustAmt = settleAdjustAmt;
	}
	//T-1日清算轧差
	public void setCearOffsetBal(double clearOffsetBal) {
		this.clearOffsetBal = clearOffsetBal;
	}
	//T-1日印花税轧差
	public void setStamptaxOffsetBal(double stamptaxOffsetBal) {
		this.stamptaxOffsetBal = stamptaxOffsetBal;
	}
	//T-1日红冲蓝补
	public void setRedBlueFillAmt(double redBlueFillAmt) {
		this.redBlueFillAmt = redBlueFillAmt;
	}
	//其他费用1
	public void setOther1Amt(double other1Amt) {
		this.other1Amt = other1Amt;
	}
	//其他费用2
	public void setOther2Amt(double other2Amt) {
		this.other2Amt = other2Amt;
	}
	//其他费用3
	public void setOther3Amt(double other3Amt) {
		this.other3Amt = other3Amt;
	}
	//其他费用4
	public void setOther4Amt(double other4Amt) {
		this.other4Amt = other4Amt;
	}
	public void setNote(String note) {
		this.note = note;
	}
	public void setBusiDate(String busiDate) {
		this.busiDate = busiDate;
	}
	public void setExpType(String expType) {
		this.expType = expType;
	}
	public void setDateType(String dateType) {
		this.dateType = dateType;
	}
	public void setFactAmtDate(String factAmtDate) {
		this.factAmtDate = factAmtDate;
	}
	public void setShFactAmt(double shFactAmt){
		this.shFactAmt = shFactAmt;
	}
	public void setSzFactAmt(double szFactAmt){
		this.szFactAmt = szFactAmt;
	}
	public void setCheckDate(String checkDate){
		this.checkDate = checkDate;
	}
	public String getCheckDate(){
		return checkDate;
	}
	public void setQuerySettleDate(String querySettleDate) {
		this.querySettleDate = querySettleDate;
	}
	public R13ChkFactAmt getR13ChkFactAmt() {
		return r13ChkFactAmt;
	}
	public SettleAmtDetail getSettleAmtDetail() {
		return settleAmtDetail;
	}
	public ListPage<DataCheckR13> getCheckR13Page() {
		return checkR13Page;
	}
	public List<DataCheckDef> getDataCheckDefs() {
		return dataCheckDefs;
	}
	/*
	public String getCurrentBusiDate() {
		return currentBusiDate;
	}
	*/
	public int getJobStatus() {
		return jobStatus;
	}
	public String getDownloadUrl() {
		return downloadUrl;
	}
	public DataCheckR13 getCheckR13() {
		return checkR13;
	}
	
	/**
	 * 进入查询页面
	 * @return
	 */
	public String initQueryCheckR13() {
		try {
			int queryDate;
			dataCheckDefs = dataCheckService.getDataCheckDefs(DataCheckDef.ON);
			if(checkDate == null || checkDate.trim().equals("")){
				checkDate = dataSubmitService.getBusiDate();
			}
			
			ProgressStatus proSta = dataSubmitService.getProgressStatus();
			jobStatus = proSta.getDataCheckStatus();
			
			queryDate = Integer.parseInt(checkDate.trim().replaceAll("-", ""));
			checkR13 = dataCheckService.getDataCheckR13(queryDate);
			return success();
		} catch(Exception e) {
			e.printStackTrace();
			return error();
		}
	}
	/**
	 * 重新查询
	 * @return
	 */
	public String reQueryCheckR13() {
		try {
			initQueryCheckR13();
			return success();
		} catch(Exception e) {
			e.printStackTrace();
			return error();
		}
	}
	public String queryR13ChkFactAmt() {
		try {
			int queryDate;
			if(factAmtDate != null && !factAmtDate.trim().equals("")){
				queryDate = Integer.parseInt(factAmtDate.trim().replaceAll("-", ""));
				r13ChkFactAmt = dataCheckService.getR13ChkFactAmt(queryDate);
			}
			return success();
		} catch(Exception e) {
			e.printStackTrace();
			return error();
		}
	}
	public String querySettleAmtDetail() {
		try {
			int queryDate;
			if(querySettleDate != null && !querySettleDate.trim().equals("")){
				queryDate = Integer.parseInt(querySettleDate.trim().replaceAll("-", ""));
				settleAmtDetail = dataCheckService.getSettleAmtDetail(dateType,queryDate);
			}
			return success();
		} catch(Exception e) {
			e.printStackTrace();
			return error();
		}
	}
	
	public String queryCheckR13() {
		QueryParameters qp = new QueryParameters();
		
		//设置分页信息
		qp.setPageNo(pageNo);
		qp.setPageSize(pageSize);
		//排序信息
		qp.addOrder("id", false);
		//参数
		if(busiDate != null && !busiDate.trim().equals("")){
			qp.addParameter("busiDate", Integer.parseInt(busiDate.trim().replaceAll("-", "")));
		}
		
		try {
			checkR13Page = dataCheckService.getDataCheckR13Page(qp);
			return success();
		} catch(Exception e) {
			e.printStackTrace();
			return error();
		}
	}
	
	/**
	 * 导出Excel
	 */
	public String exportCheckR13() {
		QueryParameters qp = new QueryParameters();
		//排序信息
		qp.addOrder("id", false);
		//参数
		if(busiDate != null && !busiDate.trim().equals("")){
			qp.addParameter("busiDate", Integer.parseInt(busiDate.trim().replaceAll("-", "")));
		}
		try {
			//生成一个随机文件夹保证文件不重复
			String randomDir = UUID.randomUUID().toString() + "/"; 
			String saveDir = FileDispatcher.getTempDir() + randomDir;//文件保存绝对路径
			String absDir = FileDispatcher.getTempAbsDir() + randomDir;//WEB访问的相对路径
			String temFileName = dataCheckService.csExpDataCheckR13(qp, saveDir, expType);
			downloadUrl = absDir + URLEncoder.encode(temFileName, SysConstants.DOWNLOAD_FILENAME_ENCODING);
		} catch(Exception e) {
			e.printStackTrace();
		}
		return success();
	}
	
	/**
	 * 修改备注
	 * @return
	 */
	public String modifyCheckR13Note() {
		try {
			DataCheckR13 r13 = dataCheckService.modifyDataCheckR13Note(id, relateId,chkType,  discAmt,
					 reservesFactAmt,  fullComm, accumAdjustAmt,  sdcCommAmt, bankComm,
					 bankInterest, reservesInterest, secInterest, withheldTaxAmt,  settleAdjustAmt, 
					 clearOffsetBal,  stamptaxOffsetBal,  redBlueFillAmt,  other1Amt,  other2Amt, 
					 other3Amt,  other4Amt,  note);
			return success(r13.getId().toString());
		} catch(Exception e) {
			e.printStackTrace();
			return error();
		}
	}
	/**
	 * 修改实际划转金额
	 * @return
	 */
	public String modifyR13ChkFactAmt() {
		try {
			int queryDate;
			if(factAmtDate != null && !factAmtDate.trim().equals("")){
				queryDate = Integer.parseInt(factAmtDate.trim().replaceAll("-", ""));
				r13ChkFactAmt = dataCheckService.modifyR13ChkFactAmt(queryDate, shFactAmt, szFactAmt);
			}
			return success();
		} catch(Exception e) {
			e.printStackTrace();
			return error();
		}
	}
}
